package com.gxa.modules.property.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.AssertUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.property.dto.ActivityToAdd;
import com.gxa.modules.property.dto.ActivityToUpdate;
import com.gxa.modules.property.entity.Activity;
import com.gxa.modules.property.service.ActivityService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "活动管理接口")
@Slf4j
public class ActivityController {
    @Autowired
    private ActivityService activityService;


    @ApiOperation(value = "活动添加")
    @PostMapping("/activity/add")
    public Result add(@RequestBody ActivityToAdd activityToAdd){
        if (activityToAdd != null){
            this.activityService.add(activityToAdd);
            return new Result().ok();
        }else {
            return new Result().error();
        }
    }

    @ApiOperation(value = "活动删除")
    @DeleteMapping("/activity/delete")
    public Result delete(@RequestParam Integer id){
        this.activityService.delete(id);
        return new Result().ok();
    }

    @ApiOperation(value = "活动更新")
    @PutMapping("/activity/update")
    public Result update(@RequestBody ActivityToUpdate activityToUpdate){
        if (activityToUpdate != null){
            this.activityService.update(activityToUpdate);
            return new Result().ok();
        }else {
            return new Result().error();
        }
    }

    @ApiOperation(value = "活动查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value = "当前是第几页",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value = "每页显示多少条",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "title",value = "活动标题",dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "communityId",value = "小区编号",dataType = "int"),

    })
    @GetMapping("/activity/list")
    public com.gxa.modules.property.utils.Result<List<Activity>> queryActivity(@RequestParam Map<String,Object> params){
        log.info("----params----{}----",params);
        AssertUtils.isBlank(String.valueOf(params),"字符串不能为空");
        com.gxa.modules.property.utils.Result<List<Activity>> r =new com.gxa.modules.property.utils.Result<>();
        try {
            PageInfo<Activity> pageInfo = activityService.queryActivity(params);
            r = com.gxa.modules.property.utils.Result.success(pageInfo.getList(), pageInfo.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            com.gxa.modules.property.utils.Result.failed();
        }
        return r;
    }
}
